/**
 * Copyright(C) com.allinpay - 通联支付 上海研发中心
 * 
 * @author 王斌
 *
 * @since 2014年2月11日
 * @description: <描述>
 * 
 */

var menuCart = new Array();    // 点餐车信息

$(document).ready(function() {
	$(".checkbox").click(function(){
	    $(this).toggleClass('checked');
	});
	
	mcart_request();
});

function groupon_select(gpoid) {
	var isChecked;
	if ($("#chk" + gpoid).hasClass("checked")) {     //如果存在 checked css，表明，现在是正在取消对其的选择；
		isChecked = false;
	}else{
		isChecked = true;
	}
	
	ajax_post_json("apis/groupon/choose", {
		"rid":r_id,
		"grouponid":gpoid,
		"operation":(isChecked)?"selected":"unselected", 
		"access_token":access_token
	}, function(){}, function(data){
		if (data.result == true) {
			(isChecked) ? addItemToCart(data.content.curItemId, data.content.curItemCount) : removeItemFromCart(data.content.curItemId);
			showprompt = (menuCart.length > 0);
		}
	});
}

function newCartItem(id, count){
	var c = new Object();
	c.id = id;
	c.count = count;
	return c;
}

function addItemToCart(id, count) {
	menuCart[menuCart.length] = newCartItem(id, count);
}

function removeItemFromCart(id) {
	var n = 0;
	var temp = new Array();
	$.each(menuCart, function(index, item) {
		if (item.id != id){
			temp[n++] = newCartItem(item.id, item.count);
		}
	});
	menuCart = [];
	menuCart = temp;
}

function findItemInCart(id) {
	var obj = null;
	$.each(menuCart, function(index, item) {
		if (item.id == id){
			obj = item;
			return false;
		}
	});
	return obj;
}

function submit2MenuCart(){
	ajax_post_json("apis/groupon/batchSync", {"rid":r_id, "items":menuCart, "access_token":access_token}, 
	function() {
		$("#mencart_btn").attr("disabled","true");
		showLoadingDiv();
	},function(data) {
	},function(xhr, textStatus, err){
	}, function(event,xhr,options) {
		hideLoadingDiv();
		goUrl($("#menucart_link").val());
	});
}

function mcart_request(){
	ajax_post_json("apis/menu/cart", {"rid": r_id, "operation": "itemscount", "access_token":access_token}, 	function(){		
	}, function(data){
		if (data.type == "data") {
			var n = 0;
			$.each(data.content, function(index, item) {
				menuCart[n++] = newCartItem(item.id, item.count);
				$("#chk"+item.id).addClass("checked");
			});
			showprompt = (menuCart.length > 0);
		}else if (data.type == "msg") {
		}
	}, function(xhr, textStatus, errorThrown){
		
	}, function(event,xhr,options) {
		
	});
}

var dArray = new Array();
function newDetailsItem(gpoid, loaded) {
	var d = new Object();
	d.gpoid = gpoid;
	d.loaded = loaded;
	return d;
}

function hasDetail(gpoid) {
	var isLoaded = false;
	$.each(dArray, function(index, item) {
		if (item.gpoid == gpoid){
			isLoaded = item.loaded;
			return false;
		}
	});
	return isLoaded;
}

function showGrouponDetails(gpoid) {
	var frame = $("#frame"+gpoid);
	var nextGo = $(frame).next();
	
	if (hasDetail(gpoid) == true) {
		nextGo.animate({
			height : 'toggle',
			opacity : 'toggle',
			duration : "slow"
		}, function(){
			$(frame).toggleClass('frame-click');
		});
	}
	else{
		loadGrouponDeatail(gpoid, function(id) {
			nextGo.animate({
				height : 'toggle',
				opacity : 'toggle',
				duration : "slow"
			}, function(){
				$(frame).toggleClass('frame-click');
			});
		});
	}
}

function loadGrouponDeatail(gpoid, fcallback){
	ajax_post_json("apis/groupon/detail", {"rid": r_id, "grouponid": gpoid, "access_token":access_token}, 	
	function(){	
		showLoadingDiv();
	}, function(data){
		if (data.type == "data") {
			var gpo = data.content;
			var detailHtml = getBaseInfoHtml(gpo.sales_price, gpo.original_price, gpo.end_time, gpo.saled_count, gpo.left_count);
			detailHtml += getMenuItemsHtml(gpo.grouponid, gpo.items);
			detailHtml += getPurchaseNotes(gpo.valid_period, gpo.need_book, gpo.precautions);
			
			var nextGo = $("#d"+gpo.grouponid);
			$(nextGo).empty();
			$(nextGo).append(detailHtml);
			
			var scroller = "#thumb-scroll" +gpo.grouponid;
			$(scroller +' ul').promptumenu({
				'width': 270,
				'height': 54,
				'columns': 5,
				'rows': 1,
				'pages': true
			});
		
			dArray[dArray.length] = newDetailsItem(gpo.grouponid, true);
			if (fcallback) fcallback(gpo.grouponid);
		}else if (data.type == "msg") {
		}
	}, function(xhr, textStatus, errorThrown){
		
	}, function(event,xhr,options) {
		hideLoadingDiv();
	});
}

/*function getBaseInfoHtml(sales_price, orig_price, gpo_endtime, saled_count) {
	var baseinfoHtml  ="";
	baseinfoHtml += "	<div class='order-T cf'> ";
	baseinfoHtml += "  <div class='G-p-total'>";
	baseinfoHtml += "       ￥" + sales_price;
	baseinfoHtml += "  </div>";
	baseinfoHtml += "  <div class='gray-c-t'>";
	baseinfoHtml += "       <p>总价值：￥"+orig_price+"</p>";
	baseinfoHtml += "           <p>结束日期：" +gpo_endtime+"</p>";
	baseinfoHtml += "  </div>";
	baseinfoHtml += "</div>";
	return baseinfoHtml;
}*/

function getBaseInfoHtml(sales_price, orig_price, gpo_endtime, saled_count, left_count) {
	var baseinfoHtml  ="";
	baseinfoHtml += "	<div class='order-T cf'> ";
	baseinfoHtml += "  <div style=' padding:10px 5px; float:left;'>";
	baseinfoHtml += "     <p><font style='color:#91ca1b; font-size:18px;'>￥</font><font style='color:#91ca1b; font-size:24px;font-weight:bold;'>" + sales_price + "</font>";
	baseinfoHtml += "     <font style='color:#999; font-size:14px;'>￥<strike>"+orig_price+"</strike></font></p>";
	baseinfoHtml += "  </div>";
	baseinfoHtml += "  <div class='gray-c-t'>";
	if (left_count > 0 ) {
		baseinfoHtml += "     <p>仅剩：<font style='color:#999; font-size:14px;'>"+left_count+"份</font></p>";
	}else{
		baseinfoHtml += "     <p class='gray-c'>已售出 "+saled_count+" 份</p>";
	}
	baseinfoHtml += "     <p>结束日期：" +gpo_endtime+"</p>";
	baseinfoHtml += "  </div>";
	baseinfoHtml += "</div>";
	return baseinfoHtml;
}

function getMenuItemsHtml(gpoid, items) {
	var itemsHtml = "";
	itemsHtml += "<table width='300px' border='1' class='Group-pur-order' cellspacing='0' cellpadding='0'>";
	itemsHtml += "<tr>";
	itemsHtml += "   <td class='G-p-left gray-c'>内容</td>";
	itemsHtml += "   <td class='G-p-center gray-c'>数量</td>";
	itemsHtml += "   <td class='G-p-right gray-c'>单价</td>";
	itemsHtml += "</tr>";
	
	$.each(items, function(index, item) {
		itemsHtml += "<tr>";
		itemsHtml += "  <td class='G-p-left'>" +item.name+ "</td>";
		itemsHtml += "  <td class='G-p-center'>" +item.count+ "份</td>";
		itemsHtml += "  <td class='G-p-right'>" +item.price+ "元</td>";
		itemsHtml += "</tr>";
	});
	itemsHtml += "</table>";
	
	itemsHtml += "<div class='G-Roll-pic'>";

	itemsHtml += "<div id='thumb-scroll"+gpoid+"' class='example_1'>";
	itemsHtml += "<div id='thumb-tray"+gpoid+"' class='thumb-tray'>";
	itemsHtml += "<div id='thumb-outline"+gpoid+"' class='thumb-outline'>";
	
	itemsHtml += "<ul	id='thumb-list"+gpoid+"' class='thumb-list'>";
	$.each(items, function(index, item) {
		itemsHtml += "      <li ><img onclick=\"javascript:show_item_pic('"+item.name+"','"+item.price+"', $(this).attr('src'))\" src='" +imgPath + "/" + item.pic + "' class='pic'/></li>";
	});
	itemsHtml += "</ul>";
	itemsHtml += "</div>";
	itemsHtml += "<div id='thumb-prev"+gpoid+"' class='thumb-prev'></div>";
	itemsHtml += "<div id='thumb-next"+gpoid+"' class='thumb-next'></div>";
	itemsHtml += "</div>";
	itemsHtml += "</div>";
	
/*	itemsHtml += "  <ul>";
	itemsHtml += "     <li class='G-p-l-arrow'></li>";
	$.each(items, function(index, item) {
		itemsHtml += "      <li><a href='#'><img src='../../static/js/" +item.pic+ "' class='pic'/></a></li>";
	});
	itemsHtml += "     <li class='G-p-r-arrow'></li>";
	itemsHtml += "   </ul>";*/
	itemsHtml += "</div>";
	
	
/*	itemsHtml += "<div class='G-Roll-pic'>";
	
	itemsHtml += "<div class='example_2'>";
	itemsHtml += "<div class='promptumenu_window'>";
	itemsHtml += "<ul class='promptmenu_ul'>";
	$.each(items, function(index, item) {
		itemsHtml += "      <li class='promptmenu_li'><img onclick=\"javascript:show_item_pic('"+item.name+"',$(this).attr('src'))\" src='../../static/js/" +imgPath + "/" + item.pic + "' class='pic'/></li>";
	});
	itemsHtml += "</ul>";
	itemsHtml += "<div class='promptumenu_nav'></div>";
	itemsHtml += "</div>";
	itemsHtml += "</div>";*/
	
	itemsHtml += "</div>";
	
	return itemsHtml;
}

function getPurchaseNotes(valid_period, booking, notes) {
	var noteHtml = "";
	noteHtml += "<table cellspacing='0' cellpadding='0' class='P-notes'>";
	noteHtml += "  <tr>";
	noteHtml += "     <td colspan='2' class='Title'>购买须知</td>";
	noteHtml += "  </tr>";
	noteHtml += "  <tr>";
	noteHtml += "     <td class='G-p-left'>有效期</td>";
	noteHtml += "     <td class='G-p-right'>"+valid_period+"</td>";
	noteHtml += " </tr>";
	noteHtml += " <tr>";
	noteHtml += "     <td class='G-p-left'>预约</td>";
	noteHtml += "      <td class='G-p-right'>" +(booking)+ "</td>";
	noteHtml += " </tr>";
	noteHtml += " <tr>";
	noteHtml += "     <td class='G-p-left u-k'>使用须知</td>";
	noteHtml += "     <td class='G-p-right'>";
	if (isValide(notes)) {
		noteHtml += "          "+notes.replace(/\n/g, "<br />");
	}
	noteHtml += "     </td>";
	noteHtml += " </tr>";
	noteHtml += "</table>";
	return noteHtml;
}

var isOk2Hide = false;
function show_item_pic(name, price, imgpath) {
	$("#pop_image").attr('src',imgpath);
	$("#pop_name").empty();
	$("#pop_name").append(name + "<font style='font-size:12px; margin-left: 10px;' > ￥" + price + "<font>");
	$("#pop_desc").html("");

	if ($("#pop_show").is(":visible")) {
		return;
	}
	
	var top = (($(window).height() / 2) - ($("#pop_show").outerHeight() / 2)) - 25;
	var left = (($(window).width() / 2) - ($("#pop_show").outerWidth() / 2));
	if( top < 0 ) top = 0;
	if( left < 0 ) left = 0;

	$("#pop_show").css({
		top: top + 'px',
		left: left + 'px'
	});
	
	$(".main").click(function(){
		if (isOk2Hide == true && $("#pop_show").is(":visible")) {
			isOk2Hide = false;
			$("#pop_show").hide();
		}
	});
	
	$("#pop_show").fadeTo(200, 1, function() {
		isOk2Hide = !isOk2Hide;
	});
}

function hidenPopShow(){
	isOk2Hide = false;
	$("#pop_show").fadeOut(200);
}

function forwardgpo(target, msg, ok, cancel) {
	if (!showprompt) {
		goUrl(target);
	}else {
		forward(target, msg, ok, cancel, function(s, t) {
			if (s == true) {
				ajax_post_json("apis/menu/cart", {"rid": r_id, "operation": "clearcart", "access_token":access_token},
				function(){
				}, function(data){
				}, function(xhr, textStatus, errorThrown){
				}, function(event,xhr,options) {
					goUrl(t);
				});
			}
		});
	}
}
